require_dependency 'map_utils'

class EventsController < ApplicationController
  include MapUtils
  helper "ecases"
  uses_tiny_mce(:only => [:show]) 
  before_filter :set_event, :only => [:show, :set_description, :change_map_location,:destroy,:save_region,:edit_title_and_date,:set_title_and_date,:return_title]

  before_filter :authorize_author, :only => [:new, :save_event_info]
  before_filter :authorize_event_owner, :only => [:set_description, 
    :save_region, :change_map_location, :destroy,:edit_title_and_date,:set_title_and_date,:return_title]

  def index
    list
    render :action => :list
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
    :redirect_to => { :action => :list }

  def show
    set_editing_layout
    pre_set_map_extended
    @ref = @commentable = @event
    @slideshow = @event.slideshow
    @comments_size = @event.comments.size.to_s
    @comments = @event.last_comments
    if @slideshow
      @slideshow_xml = url_for(:controller => "slideshows", :action => :generate_xml, :id => @slideshow.id)
    end
    if params[:editor_action_id]
      @editor_place = params[:editor_action_id]
      @editor_content = @event.send(@editor_place)
    end
    @selected_tab = :ecase
    @opened_nodes = get_opened_nodes_for_ecase
  end

  def new
    @selected_tab = :ecase
    @ecase = Ecase.find(params[:id])
    @ecase_region = @ecase.ecase_region
    set_map(@ecase_region)
    @region = nil;
  end

  def destroy
    @ecase = @event.ecase
    if !@event.slideshow.nil?
       @event.slideshow.destroy
    end      
     @event.destroy
     redirect_to :controller=>'ecases', :action => :show, :id => @ecase.id and return
  end
  
  def save_event_info
    y = params[:date][:year]
    m = params[:date][:month]
    d = params[:date][:day]
    h = params[:date][:hour] ? params[:date][:hour] : "0"
    min = params[:date][:min] ? params[:date][:min] : "0"
    if DateTime.valid_civil?(y.to_i,m.to_i,d.to_i) && DateTime.valid_time?(h.to_i,min.to_i,0) && is_str_num(y) && is_str_num(m) && is_str_num(d) && is_str_num(h) && is_str_num(min)
      @event = Event.new(params[:event])
      @event.date = datetime_str(y,m,d,h,min,0)
      if !@event.save
        render :partial=> "save_region_validation" , :locals => {:v_text => '<p>Please enter correct Title</p>'} and return
      end
    else
       render :partial=> "save_region_validation" , :locals => {:v_text => '<p>Please enter correct Date of event</p>'} and return
    end
    @cancel_controller = "events"
    @cancel_id = @event.id
  end
  
  def change_map_location
    @cancel_controller = "events"
    @ecase = @event.ecase
    @selected_tab = :ecase
    no_map = @event.region.nil?
    set_map(no_map ? @ecase.ecase_region : @event.region)
    @region = no_map ? nil : @region;
  end
  
  def save_region
   region = @event.region 
    if region
      @region = region
      @region.update_attributes(params[:region])
    else
      @region = Region.new(params[:region])
      @region.event_id =  @event.id
      @region.save
    end
  end
 
  def set_description
    if !@event.update_attribute("description", params[:editor_content])
      flash[:notice] = "Description text was not updated."
    end
    redirect_to :action => :show, :id => params[:id]
  end
  
  def edit_title_and_date
    @event_date = @event.date_time
    render :partial => "/events/edit_title_and_date"
  end
  
  def return_title
    @ecase = @event.ecase
    render :partial => "/events/title"
  end

  def set_title_and_date
    event_date = params[:event_date] 
    y = event_date[:year]
    m = event_date[:month]
    d = event_date[:day]
    h = event_date[:hour] || "0"
    min = event_date[:min] || "0"
    @ecase = @event.ecase
    if DateTime.valid_civil?(y.to_i,m.to_i,d.to_i) && DateTime.valid_time?(h.to_i,min.to_i,0) && is_str_num(y) && is_str_num(m) && is_str_num(d) && is_str_num(h) && is_str_num(min)
      @event.date = datetime_str(y,m,d,h,min,0)
      @event.title = params[:event][:title]
      if !@event.save
        set_flash_info(FlashInfo.get_error("Please enter correct Title"))
        @event_date = @event.date_time
        render :partial => "/events/edit_title_and_date" and return
      end
    else
      set_flash_info(FlashInfo.get_error("Please enter correct Date of event"))
      @event_date = @event.date_time
      render :partial => "/events/edit_title_and_date" and return
    end
    render :partial => "/events/title" 
  end

  private
  
  def is_str_num s
    s.count("0-9") == s.length
  end
    
  def pre_set_map_extended
    @region = @event.region
    @ecase = @event.ecase
    @ecase_region = @ecase.ecase_region
    set_map_extended
  end
  
  def set_event
    @event = Event.find(params[:id])
     if @event.nil?
       redirect_to :controller => "ecases", :action => :list and return
    end
  end 

  # filter before "" and "update" actions
  # authorizes user to be admin or author and owner of given ecase
  # this allows to perform several actions
  def authorize_event_owner
    return false unless authorize_author

    authorized = admin_authorized? || (get_session_user.id == (Event.find(@event.id)).author_id) 

    unless authorized
      on_not_authorized("Please login as author of this ecase.")
    end

    authorized
  end

  def get_opened_nodes_for_ecase
    get_opened_nodes[@event.ecase_id] ||= []
  end

  def get_opened_nodes
    session[:opened_nodes] ||= {}
  end
end
